Start your application when windows starts 
  
 Print  
  Email  
  
 
 
 
 Submitted on: 5/19/2001 5:04:16 AM
By: venky_dude 

Level: Beginner
User Rating:      By 4 Users
Compatibility:VB 6.0

Users have accessed this article 1482 times.
   
(About the author) 
 
  
     Many people want to know how to start there application when windows starts and not simply by putting a shortcut in the windows startup folder.This article will show you how to do this using vb and a few api's 

--------------------------------------------------------------------------------
 
 
  
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.  
We shall use a few api function which are  


Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long




These api's are used for the following purpose


1.RegOpenKey - To open a key for reading/writing values 
2.RegSetValue - To write values into a key


We shall also use a few constants 

Private Const HKEY_CURRENT_USER = &H80000001

Private Const REG_SZ = 1


In order to make our applications start when windows starts we have to add an entry in the 

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run



To make an entry into a key we need to get the 'handle' or a unique identifier for that key.We get this 'unique id by opening the key.We do this in the following way

Dim result As Long
Dim keyres As Long
result=RegOpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",keyres)


If the function executed correctly we will get 0 as result and keyres will contain the unique id for that key.

After opening the key we will put in a value into it .We can do it this way


Dim file As String
Dim entry as string
entry = "Myprog"
file = "c:\myprog\myprog.exe"
result = RegSetValueEx(keyres, entry, 0, REG_SZ, ByVal file, Len(file))


you can input your program's path into file and run the function.And entry is the name you give to the value you are trying to put in.If the function executed successfully result will contain 0 .Restart your computer and your program should start as soon as windows starts.Send your comments to venky_dude@yahoo.com .Visit my homepage for some cool vb codes. 
 
 
